package com.javaee.service.admin.impl;


import com.javaee.entity.User;

import com.javaee.mapper.admin.UserMapper;
import com.javaee.service.admin.UserService;
import com.javaee.utils.AliOssUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String toAddUser(User user, Model model) {
        model.addAttribute("user", user);
        return "admin/addUser";
    }

    @Override
    public String addUser(User user, HttpServletRequest request) {
        MultipartFile file = user.getFileName();
        String originalFilename = file.getOriginalFilename();
        String fileUrl=null;
        try {
            fileUrl = AliOssUtil.uploadFile(originalFilename,file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        user.setTx(fileUrl);
        userMapper.addUser(user);
        return "redirect:/user/selectAllUsersByPage?currentPage=1";
    }

    @Override
    public String selectAllUsersByPage(Model model, int currentPage) {
        //共多少个病人
        int totalCount = userMapper.selectCount();
        System.out.println("totalCount = " + totalCount);
        //计算共多少页
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<User> allUsers = userMapper.selectAllUsersByPage((currentPage-1)*pageSize, pageSize);
        model.addAttribute("allUsers", allUsers);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);

        return "admin/selectUsers";
    }

    @Override
    public String detail(Model model, int id) {
        model.addAttribute("user", userMapper.selectAUser(id));
        //转向查看详情
        return "admin/detailUser";

    }

    @Override
    public String delete(int id) {
//         if(关于这个病人有挂号信息>0){
//             return "不能删除因为他有挂号信息";
//         }
        userMapper.deleteUser(id);
        return "删除成功";
    }

    @Override
    public String toUpdate(Model model, Integer id) {
        User user = userMapper.selectAUser(id);
        System.out.println("在修改中查一个病人 " + user);
        model.addAttribute("user",user );
        return "admin/updateUser";
    }


    @Override
    public String update(User user, String people,HttpServletRequest request) throws Exception {

        MultipartFile file = user.getFileName();
        if(!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String fileUrl = AliOssUtil.uploadFile(originalFilename, file.getInputStream());

            user.setTx(fileUrl);
        }
        userMapper.updateUser(user);
        if(people!=null) {
            if (people.equals("u"))
                return "redirect:/userInfo/toUserInfo";
        }
        return "redirect:/user/selectAllUsersByPage?currentPage=1";
    }
}
